<?php


namespace app\common\model;


use app\common\traites\TraitModel;
use think\Model;


class UserModel extends Model
{
    use TraitModel;

    public $name = 'common_user';

    public static function onAfterDelete($user)
    {
       //用户删除事件
        if (empty($user)) return false;
        //删除用户代理信息
        AgentLavel::where('agent_id',$user->id)->delete();
        //删除用户扩展展
        UserSet::where('u_id',$user->id)->delete();
        return true;
    }

    public function getPwdAttr($value)
    {
        return '';
    }

    public function getwithdrawPwdAttr($value)
    {
        return '';
    }

    public static function page_list($where, $limit, $page, $date)
    {
         
        $map = [];
        $find = null;
        //单独查询代理商信息开始
        //如果是单独查询代理及代理 以下用户信息
        if (isset($where['agent_name'])) {
            $map[] = ['user_name', 'like', '%' . $where['agent_name'] . '%'];
            $find = self::where($map)->where('type', 1)->find();
            unset($where['agent_name']);
            if (!$find) show([]);
            //获取查询代理商信息
        }

        //单独查询代理商信息结束
        //代理商登陆只获得与代理商相关的信息开始
        $map = self::whereMapUser($find);
        //代理商登陆只获得与代理商相关的信息结束
        
        //时间查询存在
        $res = self::alias('a')->where($where)->where(function ($query) use($map) {
            $query->whereOr($map);
        });
        if (isset($date['start']) && isset($date['end'])) {
            $res = $res->whereTime('a.create_time', 'between', [$date['start'], $date['end']]);
        } elseif (isset($date['start'])) {
            $res = $res->whereTime('a.create_time', '>=', $date['start']);
        } elseif (isset($date['end'])) {
            $res = $res->whereTime('a.create_time', '<=', $date['end']);
        }
      
         return $res->join('common_admin b', 'a.market_uid = b.id', 'left')
            ->join('dianji_user_set c', 'c.u_id = a.id', 'left')
            //->field('a.*,b.user_name admin,c.xima_lv')
            ->field('
            a.*,b.user_name admin,c.xima_lv,
            bjl_xian_hong_xian_max,bjl_xian_hong_zhuang_max,bjl_xian_hong_he_max,
            bjl_xian_hong_zhuang_dui_max,bjl_xian_hong_xian_dui_max,bjl_xian_hong_lucky6_max,
            lh_xian_hong_long_max,lh_xian_hong_hu_max,lh_xian_hong_he_max,bjl_xian_hong_xian_min,
            bjl_xian_hong_zhuang_min,bjl_xian_hong_he_min,bjl_xian_hong_zhuang_dui_min,
            bjl_xian_hong_xian_dui_min,bjl_xian_hong_lucky6_min,lh_xian_hong_long_min,
            lh_xian_hong_hu_min,lh_xian_hong_he_min,is_xian_hong
            ')
            ->order('id desc')
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->each(function ($item, $key) {
                $item->tg_url_google =$item->tg_url_txt= '';
                if ($item['type'] == 1) {
                    !empty($item->invitation_code) && $item->tg_url_google = captchaUrl($item->invitation_code);
                    $item->tg_url_txt = tg_url() . $item->invitation_code;
                }
            });
             
    }

    /**
     * @param $limit
     * @param $page
     * @param null $user
     * @return mixed
     */
    public static function page_one($limit, $page)
    {
        //是代理登陆的时候查询该代理的信息
        if (session('admin_user.agent')) {
            $map['a.id'] = session('admin_user.id');
        } else {
            $map['a.type'] = 1;
        }

        return self::alias('a')
            ->where($map)
            ->join('common_admin b', 'a.market_uid = b.id', 'left')
            ->field('a.*,b.user_name admin')
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->each(function ($item, $key) {
                $item->tg_url_txt = $_SERVER['REQUEST_SCHEME'] . '://' . randomkey(5) . '.' . config('ToConfig.app_tg.tg_url') . '?code=' . $item->invitation_code;
            });
    }

    //直接删除
    public function del($id)
    {
        $find = $this->find($id);
        if (empty($find))
            return false;
        return $find->delete();
    }

    public static function user_sort($map,$order,$limit,$page)
    {
        return self::where($map)
            ->order($order)
            ->field('id,user_name,create_time,type,status,money_balance,money_total_recharge,money_total_withdraw,money_total_agent')
            ->paginate(['list_rows' => $limit, 'page' => $page], false);
    }

    public function money_log()
    {
        return $this->hasMany(MoneyLog::class,'uid','id');
    }
    public function order_log()
    {
        return $this->hasMany(OrderModel::class,'uid','id');
    }

    public function user_set()
    {
        return $this->hasMany(UserSet::class,'u_id','id');
    }
}